package com.zpxt.zpxt.serve;

import com.zpxt.zpxt.dao.ApplicationRepository;
import com.zpxt.zpxt.dao.CompanyRepository;
import com.zpxt.zpxt.dao.PositionRepository;
import com.zpxt.zpxt.entity.Application;
import com.zpxt.zpxt.entity.Company;
import com.zpxt.zpxt.entity.Position;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author:hjx
 * @Description:
 * @Date: Created in 19:43 2018/4/13
 * @Modified By:
 */
@Service("companyServe")
public class CompanyServe {
    @Autowired
    PositionRepository positionRepository;
    @Autowired
    ApplicationRepository applicationRepository;
    @Autowired
    CompanyRepository companyRepository;
//  职位新增
    public void addPosition(Position position){
        position.setReleaseTime(new Date());
        positionRepository.save(position);
    }
// 职位修改
    public void editPosition(Position position,Long companyId) throws Exception {
        Optional<Position> optional = positionRepository.findById(position.getId());
        Position position1 =optional.get();
        if(position1.getCompanyId()==companyId){
            position.setReleaseTime(new Date());
            positionRepository.save(position);
        }else{
            throw new Exception("只能修改自己发布的职位！");
        }
    }
// 职位删除
    public void delPosition(Long id,Long companyId) throws Exception {
        Optional<Position> optional = positionRepository.findById(id);
        Position position =optional.get();
        if(position.getCompanyId()==companyId){
            positionRepository.delete(position);
        }else {
            throw new Exception("只能删除自己发布的职位！");
        }
    }
// 职位列表查询
    public List<Position> findPositionList(Long companyId){
        List<Position> positions = positionRepository.findByCompanyId(companyId);
        return  positions;
    }
// 职位查询
    public Position findPositionOne(Long id) throws Exception {
        Optional<Position> optional = positionRepository.findById(id);
        Position position =optional.get();
        return  position;
    }
// 分页查询
    public Page<Position> findAll(Pageable pageable){
        Page<Position> page = positionRepository.findAll(pageable);
        List<Position> list = page.getContent();
        return page;
    }
    // 分页查询
    public Page<Position> findAllLike(String name,Pageable pageable){
        Page<Position> page = positionRepository.findAllLike(name,pageable);
        List<Position> list = page.getContent();
        return page;
    }
    //    申请列表
    public List<Map> appList(Long companyId){
        List<Map> list = applicationRepository.findAppByCompanyId(companyId);
        return  list;
    }
// 申请处理
    public void appProcess(Long appId,String process){
        Application application = applicationRepository.findById(appId).get();
        if(process.equals("pass")){
            application.setStatus(Application.ReviewAndPassThrough);
        }else if(process.equals("refuse")){
            application.setStatus(Application.HaveRefused);
        }
        application.setHandleTime(new Date());
        applicationRepository.save(application);
    }
    // 查询企业信息
    public Company findCompanyInfo(Long companyId){
        Company company =companyRepository.findById(companyId).get();
        company.setCode("");
        company.setPassword("");
        return company;
    }
}
